package com.huawang.ecolink.apps.controllers.app;

import com.huawang.ecolink.apps.common.ServiceUtil;
import com.huawang.ecolink.apps.response.BaseResponse;
import com.huawang.ecolink.core.meta.metaclass.uml.PoolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;

@Log
@Api(tags = "服务接口")
@RestController
@RequestMapping("/mlink")
public class SerivceController {

    @Autowired
    private ServiceUtil serviceUtil;

    /*** 
     * @Description: 服务优雅关闭
     * @param
     * @return: com.huawang.ecolink.apps.response.BaseResponse<java.lang.String>
     * @throws:
     */
    @ApiOperation("服务退出")
    @PostMapping("/service/exit")
    public BaseResponse<String> shutdownContext() {

        AtomicInteger exit = new AtomicInteger();

        CompletableFuture.runAsync(() ->
                exit.set(serviceUtil.exeShutDown()), PoolUtil.THREAD_POOL_TASK_EXECUTOR);
        if (exit.get() == 0) {
            return BaseResponse.success("exit success");
        } else {
            return BaseResponse.success("fail");
        }

    }

    /*** 
     * @Description: 心跳机制  查询服务是否在线
     * @param reqBody String
     * @return: com.huawang.ecolink.apps.response.BaseResponse<java.lang.String>
     * @throws:
     */
    @ApiOperation("服务询问")
    @PostMapping("/modelica/hello")
    public BaseResponse<String> modelicaHello(@RequestBody String reqBody) {
        return BaseResponse.success("success");
    }
}
